//package cn.cttic.app.service.impl;
//
//import cn.dev33.satoken.exception.NotLoginException;
//import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import cn.cttic.app.api.domain.BaseUser;
//import cn.cttic.app.api.domain.bo.AppUserBo;
//import cn.cttic.app.api.domain.vo.AppUserVo;
//import cn.cttic.app.mapper.AppUserMapper;
//import cn.cttic.app.service.IAppUserService;
//import cn.cttic.common.core.utils.StringUtils;
//import cn.cttic.common.mybatis.core.page.PageQuery;
//import cn.cttic.common.mybatis.core.page.TableDataInfo;
//import cn.cttic.common.satoken.utils.StpUserUtil;
//import lombok.RequiredArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.util.*;
//
///**
// * app用户Service业务层处理
// *
// * @author 莫硕文
// * @date 2024-01-03
// */
//@RequiredArgsConstructor
//@Service
//@Slf4j
//public class AppUserServiceImpl implements IAppUserService {
//
//    private final AppUserMapper baseMapper;
//
//    @Override
//    public String selectUserNameById(String id) {
//        return baseMapper.selectVoById(id).getUserName();
//    }
//
//    /**
//     * 查询app用户
//     */
//    @Override
//    public AppUserVo queryById(String id) {
//        return baseMapper.selectVoById(id);
//    }
//
//    /**
//     * 查询app用户列表
//     */
//    @Override
//    public TableDataInfo<AppUserVo> queryPageList(AppUserBo bo, PageQuery pageQuery) {
//        LambdaQueryWrapper<BaseUser> lqw = buildQueryWrapper(bo);
//        Page<AppUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
//        return TableDataInfo.build(result);
//    }
//
//    /**
//     * 查询app用户列表
//     */
//    @Override
//    public List<AppUserVo> queryList(AppUserBo bo) {
//        LambdaQueryWrapper<BaseUser> lqw = buildQueryWrapper(bo);
//        return baseMapper.selectVoList(lqw);
//    }
//
//    private LambdaQueryWrapper<BaseUser> buildQueryWrapper(AppUserBo bo) {
//        Map<String, Object> params = bo.getParams();
//        LambdaQueryWrapper<BaseUser> lqw = Wrappers.lambdaQuery();
//        lqw.like(StringUtils.isNotBlank(bo.getUserName()), BaseUser::getUserName, bo.getUserName());
//        lqw.like(StringUtils.isNotBlank(bo.getRealName()), BaseUser::getRealName, bo.getRealName());
//        lqw.eq(StringUtils.isNotBlank(bo.getIdCardNo()), BaseUser::getIdCardNo, bo.getIdCardNo());
//        lqw.like(StringUtils.isNotBlank(bo.getNickName()), BaseUser::getNickName, bo.getNickName());
//        lqw.eq(StringUtils.isNotBlank(bo.getSalt()), BaseUser::getSalt, bo.getSalt());
//        lqw.eq(StringUtils.isNotBlank(bo.getPassword()), BaseUser::getPassword, bo.getPassword());
//        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), BaseUser::getPhone, bo.getPhone());
//        lqw.eq(bo.getStatus() != null, BaseUser::getStatus, bo.getStatus());
//        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), BaseUser::getAvatar, bo.getAvatar());
//        lqw.eq(StringUtils.isNotBlank(bo.getAttach()), BaseUser::getAttach, bo.getAttach());
//        lqw.eq(bo.getSort() != null, BaseUser::getSort, bo.getSort());
//        lqw.eq(StringUtils.isNotBlank(bo.getUniCloudId()), BaseUser::getUniCloudId, bo.getUniCloudId());
//        lqw.eq(StringUtils.isNotBlank(bo.getWechatOpenId()), BaseUser::getWechatOpenId, bo.getWechatOpenId());
//        lqw.eq(StringUtils.isNotBlank(bo.getQqOpenId()), BaseUser::getQqOpenId, bo.getQqOpenId());
//        return lqw;
//    }
//
//    /**
//     * 新增app用户
//     */
//    @Override
//    public Boolean insertByBo(AppUserBo bo) {
//        BaseUser add = BeanUtil.toBean(bo, BaseUser.class);
//        validEntityBeforeSave(add);
//        boolean flag = baseMapper.insert(add) > 0;
//        if (flag) {
//            bo.setId(add.getId());
//        }
//        return flag;
//    }
//
//    /**
//     * 修改app用户
//     */
//    @Override
//    public Boolean updateByBo(AppUserBo bo) {
//        BaseUser update = BeanUtil.toBean(bo, BaseUser.class);
//        validEntityBeforeSave(update);
//        return baseMapper.updateById(update) > 0;
//    }
//
//    /**
//     * 保存前的数据校验
//     */
//    private void validEntityBeforeSave(BaseUser entity) {
//        //TODO 做一些数据校验,如唯一约束
//    }
//
//    /**
//     * 批量删除app用户
//     */
//    @Override
//    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
//        if (isValid) {
//            //TODO 做一些业务上的校验,判断是否需要校验
//        }
//        return baseMapper.deleteBatchIds(ids) > 0;
//    }
//
//    /**
//     * 主动注销账号
//     *
//     * @param id 账号id
//     */
//    @Override
//    public void deleteAccount(String id) {
//
//        // TODO 主动注销之后，再次登录视为新用户
//        // 1. 逻辑删除用户本身信息
//        // 2. 判断是否有未完结交易
//        // 3. 逻辑删除所绑定的会员，卡，卷 类信息
//
//        this.baseMapper.deleteById(id);
//
//        // TODO 踢app用户下线,待与sa-token的多账号体系结合
//        try {
//            StpUserUtil.logout(id);
//        } catch (NotLoginException ignored) {
//        }
//    }
//
//    /**
//     * 上传头像
//     *
//     * @param file 头像文件
//     * @return
//     */
//    @Override
//    public int updateAvatar(MultipartFile file) {
//
////        AppLoginUser loginUser = AppLoginHelper.getAppLoginUser();
////
////        try {
////            RemoteFile sysFile = remoteOssService.upload(new String(Base64.getEncoder().encode(file.getBytes()), StandardCharsets.UTF_8), file.getOriginalFilename(), file.getContentType());
////            sysFile = remoteOssService.confirmUpload(sysFile.getOssId());
////            int updated = baseMapper.update(
////                new UpdateWrapper<BaseUser>().eq("id", loginUser.getUserId()).set("avatar", sysFile.getUrl())
////            );
////            //每次修改头像 插入头像审核记录表为待审核
////            if (updated > 0) {
////                //先查询当前userid是否存在记录 存在就修改 无则新增
////                AppUserAvatarModifyVo appUserAvatarModifyVo = iAppUserAvatarModifyService.selectByUserId(loginUser.getUserId());
////                if (appUserAvatarModifyVo == null) {
////                    AppUserAvatarModifyBo appUserAvatarModifyBo = new AppUserAvatarModifyBo();
////                    appUserAvatarModifyBo.setUserId(loginUser.getUserId());
////                    appUserAvatarModifyBo.setCreateBy(loginUser.getUserId());
////                    appUserAvatarModifyBo.setCreateTime(new Date());
////                    appUserAvatarModifyBo.setOldAvatarOssId(sysFile.getOssId());
////                    appUserAvatarModifyBo.setOldAvatarUrl(sysFile.getUrl());
////                    appUserAvatarModifyBo.setNewAvatarOssId(sysFile.getOssId());
////                    appUserAvatarModifyBo.setNewAvatarUrl(sysFile.getUrl());
////                    appUserAvatarModifyBo.setAuditStatus(AuditStatus.AUDITING.getValue());
////                    iAppUserAvatarModifyService.insertByBo(appUserAvatarModifyBo);
////                } else {
////                    AppUserAvatarModifyBo appUserAvatarModifyBo = new AppUserAvatarModifyBo();
////                    BeanUtil.copyProperties(appUserAvatarModifyVo, appUserAvatarModifyBo);
////                    appUserAvatarModifyBo.setUserId(loginUser.getUserId());
////                    appUserAvatarModifyBo.setOldAvatarOssId(appUserAvatarModifyVo.getNewAvatarOssId());
////                    appUserAvatarModifyBo.setOldAvatarUrl(appUserAvatarModifyVo.getNewAvatarUrl());
////                    appUserAvatarModifyBo.setNewAvatarOssId(sysFile.getOssId());
////                    appUserAvatarModifyBo.setNewAvatarUrl(sysFile.getUrl());
////                    appUserAvatarModifyBo.setUpdateBy(loginUser.getUserId());
////                    appUserAvatarModifyBo.setUpdateTime(new Date());
////                    appUserAvatarModifyBo.setAuditStatus(AuditStatus.AUDITING.getValue());
////                    iAppUserAvatarModifyService.updateByBo(appUserAvatarModifyBo);
////                }
////            }
////            return updated;
////        } catch (IOException e) {
////            return 0;
////        }
//        return 0;
//    }
//
//
//}
